﻿/**
* 前端公共js
*/


$(function(){
	
	
var ua = navigator.userAgent;
var ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
isIphone =!ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
isAndroid = ua.match(/(Android)\s+([\d.]+)/),
isMobile = isIphone || isAndroid;
if(isMobile){
	window.location='/Mobile/index';
}
	
	
	
	
	$(".conkfbtn").click(function(){
		$(".nb-group-text").eq(1).click();		
	});
	

	
	//显示注册弹窗
	function register(){
		closeLoginBox();
		var pos = getBoxWidth();

		var html = '<div class="login-shade-box"></div>';
		html += '<div class="login-box" style="width:' + pos[0] + 'px;left:' + pos[1] + 'px">';
		html +=		'<div class="close-btn-box" title="关闭" onclick="closeLoginBox()"><span class="glyphicon glyphicon-remove-circle"></span></div>';
		html +=		'<div class="logo-box"></div>';
		html +=		'<div class="ccz-form">';
		html +=			'<div class="ccz-input-box">';
		html +=				'<input type="text" name="mobile" class="ccz-input short-input" placeholder="手机号" onfocus="userInput($(this), 1)" onblur="userInput($(this), 2)"/>';
		html +=				'<div class="get-code-box J_get_mobile_code" onclick="getCode($(this))" data-flag="1">获取验证码</div>';
		html +=			'</div>';
		html +=			'<div class="ccz-input-box">';
		html +=				'<input type="text" name="code" class="ccz-input" placeholder="验证码" onfocus="userInput($(this), 1)" onblur="userInput($(this), 2)"/>';
		html +=			'</div>';
		html +=			'<div class="pro-login-box">';
		html +=				'<div class="model model-checkbox" onclick="check_box($(this))" data-status="2"><span class="glyphicon glyphicon-ok"></span></div>';
		html +=				'<div class="model model-pro">我已阅读并同意相关<span title="点击查看" onclick="showUserPro($(this))" data-url="">用户协议</span></div>';
		html +=				'<div class="model model-login" title="已有账户点击登录" onclick="login()">账号登录</div>';
		html +=			'</div>';
		html +=			'<button class="czz-login-btn" onclick="exeRegister($(this))" data-pro="" data-status="1">注册</button>';
		html +=		'</div>';
		html += '</div>';
		$('body').append(html);
	
	}
	//显示登录弹窗
	function login(){
		closeLoginBox();
		var pos = getBoxWidth();

		var html = '<div class="login-shade-box"></div>';
		html += '<div class="login-box" style="width:' + pos[0] + 'px;left:' + pos[1] + 'px">';
		html +=		'<div class="close-btn-box" title="关闭" onclick="closeLoginBox($(this))"><span class="glyphicon glyphicon-remove-circle"></span></div>';
		html +=		'<div class="logo-box"></div>';
		html +=		'<div class="ccz-form">';
		html +=			'<div class="ccz-input-box">';
		html +=				'<input type="text" name="mobile" class="ccz-input" placeholder="手机号" onfocus="userInput($(this), 1)" onblur="userInput($(this), 2)"/>';
		html +=			'</div>';
		html +=			'<button class="czz-login-btn" onclick="exeLogin($(this))" data-status="1" style="margin:0;margin-top:20px">登录</button>';
		html +=			'<div class="go-to-register" onclick="register()">没有账号，点击注册</div>';
		html +=		'</div>';
		html += '</div>';
		$('body').append(html);
	}
	//用户登录
	function exeLogin(_this){
		var status = parseInt(_this.attr('data-status'));
		if(status == 1){
			var mobile = _this.parents('.login-box').find('input[name=mobile]').val();
			if(!mobile){
				layer.msg('请输入手机号');return false;
			}

			_this.text('登录中...');
			_this.attr('data-status', 2);

			setTimeout(function(){
				//执行ajax登录操作
			}, 600);
		}else{
			return false;
		}
	}
	//用户注册
	function exeRegister(_this){
		var status = parseInt(_this.attr('data-status'));
		if(status == 1){
			var par = _this.parents('.login-box');
			var pro = parseInt(_this.attr('data-pro'));
			var mobile = par.find('input[name=mobile]').val();
			var code = par.find('input[name=code]').val();
			if(pro != 2){
				layer.alert('你还未同意用户协议，暂不能注册');return false;
			}
			if(!mobile){
				layer.msg('请输入手机号');return false;
			}
			if(!code){
				layer.msg('请输入手机验证码');return false;
			}

			_this.text('注册中...');
			_this.attr('data-status', 2);

			setTimeout(function(){
				//执行ajax注册操作
			}, 600);
		}else{
			return false;
		}
	}
	//关闭弹窗
	function closeLoginBox(){
		$('body').find('.login-box, .login-shade-box').remove();
	}
	//获取弹窗宽度、位置
	function getBoxWidth(){
		var width = 488;//登录注册弹窗宽度
		var _left = parseInt($(window).width()) / 2 - width / 2;//设置弹窗屏幕居中

		var _return = new Array(width, _left);
		return _return;
	}
	//弹窗内输入框获取焦点、失去焦点事件
	function userInput(_this, type){
		if(type == 1){
			_this.addClass('inputing');
			_this.parent().addClass('inputing');
			_this.siblings().addClass('inputing');
		}else{
			_this.removeClass('inputing');
			_this.parent().removeClass('inputing');
			_this.siblings().removeClass('inputing');
		}
	}
	//复选框事件
	function check_box(_this){
		var status = parseInt(_this.attr('data-status'));
		var btn = _this.parents('.login-box').find('button');
		if(status == 1){
			_this.find('span').show();
			_this.css('border', '1px solid #0390ff');
			status = 2;
			btn.attr('disabled', false);
			btn.removeClass('disabled');
		}else if(status == 2){
			_this.find('span').hide();
			_this.css('border', '1px solid #cecece');
			status = 1;
			btn.attr('disabled', true);
			btn.addClass('disabled');
		}
		_this.attr('data-status', status);
		btn.attr('data-pro', status);
	}

	
	var curCount = 60;//当前剩余秒数
	//发送短信验证码
	function getCode(_this){
		var flag = parseInt(_this.attr('data-flag'));
		var par = _this.parents('.login-box');
		if(flag == 1){
			var mobile = par.find('input[name=mobile]').val();
			var url = "{:U('sendCode')}";
			if(!mobile){
				layer.msg('请输入手机号');return false;
			}
			var $data = {'mobile':mobile, 'flag':'mod-pwd'};

			_this.attr("data-flag", "2");
			setRemainTime(_this);

//			$.getJSON(url, $data, function(data){
//				if(data.error > 0){
//					curCount = 0;
//					layer.alert(data.info, ' ', '我知道了');
//				}else{
//					layer.msg('已发送');
//				}
//			});
		}else{
			return false;
		}
	}
	function setRemainTime(_this){
		if(curCount == 0){
			resetSendBtn(_this);
		}else{
			curCount--;
			_this.text(curCount + 's');
			setTimeout(function(){
				setRemainTime(_this);
			}, 1000);
		}
	}
	function resetSendBtn(_this){
		_this.attr('data-flag', 1);
		_this.text("获取验证码");
		curCount = 60;
	}
	
	function move(){
		var _dom = document.getElementById("total");
		var end_number = 30000;//结束时的数字
		var start_number = 0;//开始的数字
		var time = 1000;  //所用时间 1000毫秒（ 在1秒内 数值增加到d）;
		var outTime = 0;  //所消耗的时间
		var interTime = 30;
		var timer = setInterval(function(){
			outTime += interTime;
			if(outTime < time){
				_dom.innerHTML = parseInt(end_number / time * outTime);
			}else{
				_dom.innerHTML = end_number;
			}
		}, interTime);
	}
});
//核名前登录
function forLogin(){

	//$('#reg-btn').click();
	var cityName = $("#hot-city").val();
	var category = $("#hot-industry").val();
	//TODO cityName 与 category 的空 ""校验
	if (cityName == "" ){
		layer.msg("请输入城市")
	} else if(category == "") {
		layer.msg("请输入行业分类")
	}
	
	
	var resultName = myAjax('/pc/enterprisenamelib/names', {"token": localStorage.getItem('token'),"cityName":cityName,"category":category}, 'GET');
	console.log(resultName);
 
	if(resultName.code==0){ 
		var lists=[];//存放任务列表数据
	   console.log(lists);
		var data= resultName.data;
		console.log(data);
		$('.resultList').show();
		$("html,body").animate({scrollTop:$("#resultList").offset().top}, 100);

		lists=data;
		console.log(lists);
		
		var qList=$(".qiminnglist")
			var html=template("resultTPL",{
			list:lists
			})
			console.log(html);
			qList.html(html);
		
	}

}

//检测是否是中文字符串
function isChinese(text){ 
	var patrn= /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi; 
	if(!patrn.exec(text)){ 
		return false; 
	}else{ 
		return true; 
	} 
}


